﻿using System.Collections;
using UnityEngine;
using UnityEngine.AI;

public class MonsterStatePursuit : FSMState
{
    private MonsterController monsterController => fsm.role as MonsterController;

    public override void Enter()
    {
        monsterController.agent.stoppingDistance = 1;
    }

    public override void Exit()
    {

    }

    public override void Update()
    {
        MonsterController monster = monsterController;

        if (monster.isInAction || monster.hp <= 0)
            return;

        monster.FindTarget();

        if (monster.agent.pathStatus == NavMeshPathStatus.PathPartial
            || monster.agent.pathStatus == NavMeshPathStatus.PathInvalid)
        {
            fsm.SetState("Return");
            return;
        }

        if (monster.target == null)
        {
            monster.ExicFight();
            fsm.SetState("Return");
            return;
        }

        float toTarget = monster.Distance(monster.target);
        if (toTarget <= monster.attackDistance)
        {
            monster.transform.LookAt(monster.target.transform);
            int a = Random.Range(1,3);  
            monster.PlayAttack(a);
        }
        else
        {
            monster.MoveCloseTarget();
        }

        //if (monster.Hp < 0.3 && monster.isBoss == false)
        //{
        //    fsm.SetState("Flee");
        //    return;
        //}

    }
}
